<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>代码生成</title>
</head>
<body>
	<style type="text/css">
	//更改表格标题文字大小
	.datagrid-header span {     font-size: 25px !important; }  
	.datagrid-header-row, .datagrid-row{height: 55px !important; }
	.datagrid-cell span{
		font-size: 15px !important; 
	}
	.datagrid-cell{
	    text-align: center !important;
		padding:5px 15px !important;
		font-size: 15px !important; 
	}
	#dgTable{
	}
	.select-table{
	    padding: 20px 40px 20px !important;
	    margin: 0;
	}
	#formDiv{
		display:none;
		background: #eee;
		padding-bottom:10px;
	}
	.default-padding{
		padding:0 !important;
	}
	</style>
	<div class="select-table jumbotron">
		<h3>代码自动生成控件</h3>
		<p>选择数据库中业务相关表，系统相关表在这里将会被过滤不可选择，毕竟整个系统是基于这些表的。填写需要自动生成的实体名称（请填入中文，比如XX管理，那么你就填入XX吧）；通过选择框确定字段是否作为搜索字段、列表字段、表单字段；填写工程所在路径；点击生成按钮，刷新工程看看吧！</p>
		<div class="dropdown ">
		  <button class="btn btn-success btn-lg dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
		    选择表
		    <span class="caret"></span>
		  </button>
		  <ul class="dropdown-menu" id="tableOption" aria-labelledby="dropdownMenu1">
		  </ul>
		</div>
	</div>
	<div id="formDiv" >
		<form class="form-horizontal" id="editTabelForm" method="post" action="">
		  <input type="hidden" name="tabelName" id="tabelName" value=""/>
		  <input type="hidden" name="searchColumn" id="searchColumn" value=""/>
		  <input type="hidden" name="listColumn" id="listColumn" value=""/>
		  <input type="hidden" name="formColumn" id="formColumn" value=""/>
		  <div class="form-group">
		    <label for="name" class="col-sm-2 control-label">实体名称</label>
		    <div class="col-sm-7">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input  universal-class="validate[required,maxSize[7]]"  autocomplete="off" type="text" class="form-control" value="" id="name" name="name" placeholder="实体名称">
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="name" class="col-sm-2 control-label">controller路径</label>
		    <div class="col-sm-7">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input  universal-class="validate[required,maxSize[20]]"  autocomplete="off" type="text" class="form-control" value="" id="url" name="url" placeholder="controller路径,比如/Order">
		    </div>
		  </div>
		  <div class="form-group">
		    <label for="formColumn" class="col-sm-2 control-label">工程路径</label>
		    <div class="col-sm-7">
	    		<input style="display:none"><!-- for disable autocomplete on chrome -->
		      	<input  universal-class="validate[required]" autocomplete="off" type="text" class="form-control" value="" id="projectPath" name="projectPath" placeholder="填写工程硬盘路径">
		    </div>
		  </div>
		  <div class="form-group">
		    <div class="col-sm-offset-2 col-sm-7">
		      <button type="button" onclick="checkForm();" class="btn btn-primary">保存</button>
		      <button type="button" onclick="menuback()" class="btn btn-default">返回</button>
		    </div>
		  </div>
		</form>
	</div>
	<div class="default-padding container-fluid">
		<div id="dgTable">
			<table id="dg" >
			</table>
		</div>
	</div>
	
	<script type="text/javascript">
	$(function(){
		
		var isCanSubmit = 1;
		jQuery("#editTabelForm").validationEngine(
		'attach',
		{
			promptPosition : "topLeft",
			onSuccess : function() {
				if (isCanSubmit == 1) {
					jQuery("#editTabelForm").submit();
					isCanSubmit++;
				}
		    }
	    });
		window.checkForm = function(){
			if($("#editTabelForm").validationEngine("validate")){
				setValue();
				var patt = /^[a-zA-Z]:(\\([0-9a-zA-Z]+))+$|(\^\/([0-9a-zA-Z]+))+$;///^([a-zA-Z]:((\\[0-9a-zA-Z\u4e00-\u9fa5]*)*)$)|((^\/[0-9a-zA-Z\u4e00-\u9fa5]+)+)$/;
				//console.log($(projectPath).val());
				if(!patt.test($(projectPath).val())){
					$("#projectPath").validationEngine("showPrompt","请输入正确的磁盘路径","error");
				}else{
					if($(listColumn).val().trim()==""){
						AdminCommon.popWarnning("请选择列表展示字段，列表展示字段必须选择！")
					}else if($(formColumn).val().trim()==""){
						AdminCommon.popWarnning("请选择表单字段，表单字段必须选择！")
					}else{
						ajaxFormPost("/GeneratorCode/generatorCode","editTabelForm",function(){AdminCommon.refreshTab('/GenerateCode')});
					}
				}
			}
		}
		
		function setValue(){
			$(searchColumn).val(getColumn('search'));
			$(listColumn).val(getColumn('list'));
			$(formColumn).val(getColumn('form'));
		}
		
		function getColumn(type){
			var searchColumns = $("."+type+"ColumnBox:checked")
			let columnsStr = "";
			for(let i=0;i<searchColumns.length;i++){
				columnsStr = columnsStr + (i>0?",":"")+$(searchColumns[i]).attr("attr");
			}
			return columnsStr;
		}
		function stopBubble(event){
		    var e=arguments.callee.caller.arguments[0]||event;
		    if(e&&e.stopPropagation){
		        e.stopPropagation()
		    }else if(window.event){
		        window.event.cancelBubble=true;
		    }
		}
		function stopDefault(event){
		    var e=arguments.callee.caller.arguments[0]||event;
		    //阻止默认浏览器动作(W3C)
		    if(e&&e.preventDefault){
		        e.preventDefault();
		    }else{
		        //IE中阻止函数默认动作的方式
		        window.event.returnValue=false;      
		    }
		    return false;
		}
		function getType(type){
			switch(type){
			case 'VARCHAR':
				return '字符串';
				break;
			case 'DATETIME':
				return '日期';
				break;
			case 'DOUBLE':
				return '小数';
				break;
			case 'INT':
				return '整数';
				break;
			case 'TEXT':
				return '文本';
				break;
			case 'TINYINT':
				return '小整数';
				break;
			}
			return type;
		}
		window.loadTable = function(tableName){
			AdminCommon.ajaxDataPost("/GeneratorCode/getTableInfo",{tableName:tableName},false,function(d){
				$("#tabelName").val(tableName);
				$("#formDiv").slideDown('slow');
				$(".jumbotron p").slideUp('slow');
				$('#dg').datagrid({
					rownumbers:true,
					fitColumns:true,
					rownumberWidth:80,
					singleSelect:true,
					onClickRow:function(index,row){
						$("#dg").datagrid('clearSelections');
					},
					/* toolbar:"#tb", */
				    columns:[[
				        {field:'columnName',title:'字段名称'},
				        {field:'commont',title:'字段注释',
				        		formatter: function(value,row,index){
				        			if(row.primaryKey)
				        				return "主键";
								return (value&&(value==""?"无注释":value))||"无注释";
							}	
				        },
				        {field:'primaryKey',title:'是否主键',
				        		formatter: function(value,row,index){
								if (row.primaryKey){
									return "是";
								} else {
									return "否";
								}
							}	
				        },
				        {field:'nullAble',title:'是否为空',
				        		formatter: function(value,row,index){
								if (row.nullAble){
									return "是";
								} else {
									return "否";
								}
							}	
				        },
				        {field:'type',title:'字段类型',
				        		formatter: function(value,row,index){
								return getType(value);
							}	
				        },
				        {field:'search',title:'设置为搜索字段',
					        	formatter: function(value,row,index){
								if (!row.primaryKey){
									return '<input type="radio" name="searchColumnBox" class="searchColumnBox" attr="'+row.columnName+'">'
								} else {
									return '主键不可设置';
								}
							}
				        },
				        {field:'list',title:'设置为列表字段',
					        	formatter: function(value,row,index){
								if (!row.primaryKey){
									return '<input type="checkbox" class="listColumnBox" attr="'+row.columnName+'">'
								} else {
									return '主键不可设置';
								}
							}
				        },
				        {field:'form',title:'设置为表单字段',
					        	formatter: function(value,row,index){
								if (!row.primaryKey){
									return '<input type="checkbox" class="formColumnBox" attr="'+row.columnName+'">'
								} else {
									return '主键不可设置';
								}
							}
				        },
				    ]],
				    data:d,
				    rowStyler: function(index,row){
				    		return 'font-size:14px;height:35px'; // return inline style
					}
				});
			});
		}
		AdminCommon.ajaxDataPost("/GeneratorCode/getTables",{},false,function(d){
			for(let i=0;i<d.length;i++){
				let tOption = $("<li><a href='#' onclick='loadTable(\""+d[i]+"\")'>"+d[i]+"</a></li>")
				$("#tableOption").append(tOption);
			}
		});
	});
	</script>
</body>
</html>